package com.eshealthy.controller;

import com.eshealthy.entity.Cases;
import com.eshealthy.entity.Doctor;
import com.eshealthy.entity.Patient;
import com.eshealthy.entity.PatientSchedule;
import com.eshealthy.service.PatientService;
import com.eshealthy.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("patient")
@Slf4j
public class patientController {
@Autowired
private PatientService patientService;
///*
// 预约挂号
// */
    @GetMapping("addSched")
    public Map<String,Object>addSchedule(String pid, String did, String schedule){
        log.info("当前预约的信息："+pid+did+schedule);

        Map<String, Object>map = new HashMap<>();
        try{
           patientService.addSchedule(pid,did,schedule);
            map.put("state",true);
            map.put("msg","预约成功");
        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","注册失败，请重新尝试");
        }
        return map;
    }
/*
* 登录*/
    @PostMapping("login")
    public Map<String, Object> login(@RequestBody Patient patient){
        log.info("当前登录的用户信息：",patient.toString());

        Map<String, Object>map = new HashMap<>();
        try{
            Patient patientDB =patientService.login(patient);
            Map<String,String> payload=new HashMap<>();
            payload.put("id",patientDB.getId());
            payload.put("name",patientDB.getName());
            String token= JWTUtils.getToken(payload);
            map.put("token",token);
            map.put("state",true);
            patientDB.setName("");
            patientDB.setPassword("");
            map.put("msg","用户登录成功");
            map.put("patient",patientDB);

        }catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","输入错误，请重新登录");
        }
        return map;
    }
    /*
     * 注册
     * */
    @PostMapping("register")
    public Map<String,Object> register(@RequestBody Patient patient, String code, HttpServletRequest request){
        log.info("用户输入的信息[{}]"+patient.toString());
        log.info("用户输入的验证码信息[{}]"+code);
        Map<String,Object> map = new HashMap<>();
        try{
            String key = (String) request.getServletContext().getAttribute("code"); //正确验证码
            if(key.equalsIgnoreCase(code)){
                patientService.register(patient);
                map.put("state",true);
                map.put("msg","注册成功");
            }else{
                throw new RuntimeException("验证码错误");
            }
        }
        catch(Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","提示："+e.getMessage());
        }
        return map;
    }
    /*
    *预约记录
    * */
    @GetMapping("find")
    public Map<String,Object> findAll(Integer page,Integer rows,Integer id) {
        page = page==null?1:page;
        rows = rows==null?4:rows;
        HashMap<String,Object> map=new HashMap<>();
        List<PatientSchedule> patientSchedules= patientService.find(page, rows,id);
        map.put("status",true);
        map.put("msg","请求成功");
        Integer totals=patientService.findTotalsSchedule(id);
        Integer totalsPage=totals%rows==0?totals/rows:totals/rows+1;
        map.put("patientSchedules",patientSchedules);
        map.put("totals",totals);
        map.put("totalPage",totalsPage);
        map.put("page",page);
        return map;
    }
    @GetMapping("findCase")
    public Map<String,Object>findCase(Integer page, Integer rows,String pid) throws IOException {
        page = page == null ? 1 : page;
        rows = rows == null ? 4 : rows;
        HashMap<String, Object> map = new HashMap<>();
        List<Cases> cases = patientService.findCases(page, rows,pid);
        Integer totals = (int)patientService.findTotalsCase(pid);
//        System.out.println(totals);
        Integer totalsPage = totals % rows == 0 ? totals / rows : totals / rows + 1;
        map.put("cases", cases);
        map.put("totals", totals);
        map.put("totalPage", totalsPage);
        map.put("page", page);
        return map;
    }
}
